package com.team.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.github.pagehelper.PageInfo;
import com.team.entity.PageParameter;
import com.team.entity.SysUsers;
import com.team.entity.Users;
import com.team.service.SysUsersService;
import com.team.util.*;
import com.wf.captcha.utils.CaptchaUtil;
import org.apache.commons.fileupload.util.LimitedInputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")
public class SysUserController {
    @Autowired
    private SysUsersService sysUsersService;
    @RequestMapping("login")
    @ResponseBody
    public Result<SysUsers> login(String username, String password,String captcha, HttpSession session,HttpServletRequest request){
        if (CaptchaUtil.ver(captcha,request)){
            //验证码输入正确，再进行登录验证
            SysUsers sysUsers1=sysUsersService.login(username,password);
            if (sysUsers1!=null){
                session.setAttribute("user",sysUsers1);
                return new Result<SysUsers>("1000","登录成功",sysUsers1);
            }else {
                return new Result<SysUsers>("1001","用户名密码输入错误",null);
            }
        }else{
            return new Result<SysUsers>("1002","验证码输入错误",null);
        }

    }
    @RequestMapping("getAllUsers")
    @ResponseBody
    public Result<Map<String,Object>> getAllUsers(PageParameter pageParameter){
        PageInfo<Users> pageInfo=sysUsersService.getAllUsers(pageParameter);
        Map<String,Object> map=new HashMap<>();
        map.put("pageTotal",pageInfo.getTotal());
        map.put("list",pageInfo.getList());
        return new Result<Map<String,Object>>("1","查询成功",map);
    }
    @RequestMapping("addUser")
    @ResponseBody
    public Result<String> addUser(SysUsers user){
        Date date=new Date(System.currentTimeMillis());
        user.setCreatedTime(date);
        boolean flag= sysUsersService.addUser(user);
        if (flag){
            return new Result<>("1000","添加成功");
        }else {
            return new Result<>("1001","添加失败");
        }
    }
    @RequestMapping("upload")
    @ResponseBody
    public Result<String> upload(@RequestParam("file") CommonsMultipartFile file) throws IOException {
        String fileName=file.getOriginalFilename();
        String extName=fileName.substring(fileName.lastIndexOf("."));
        String newFileName=System.currentTimeMillis()+extName;
        String sqlPath= OSSUtils.upload_File(newFileName,(FileInputStream) file.getInputStream());
        return new Result<>("1000","上传成功",sqlPath);
    }
    @RequestMapping("/logout")
    @ResponseBody
    public Result logout(HttpSession session){
        session.invalidate();
        return new Result<>("200","退出成功");
    }
    @RequestMapping("/update")
    @ResponseBody
    public Result<UserForm> update(@RequestBody UserForm userForm){
        System.out.println(userForm.toString());
        if (userForm.getPassword().equals(userForm.getCheckPass())){
            int temp=sysUsersService.updateUser(userForm.getId(), MD5Utils.md5Encrypt(userForm.getPassword()),userForm.getHeadUrl());
            if (temp>0){
                return new Result<>("200","更新成功",userForm);
            }else {
                return new Result<>("500","更新失败");
            }
        }else {
            return new Result<>("500","密码不一致");
        }

    }
    @RequestMapping("getMenu")
    @ResponseBody
    public Result<List<MenuDto>> getMenu(Long userId){
        //调用业务
        List<MenuDto> list =sysUsersService.getUserMenu(userId);
        return new Result<>("1","获取菜单",list);
    }

}
